<template>
  <div :class="className" :id="id" :style="{height:height,width:width}" />
</template>

<script type="text/ecmascript-6">
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'piechart2'
    },
    id: {
      type: String,
      default: 'piechart2'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))

      this.chart.setOption({
        grid: {
          top: 0,
          left: 0,
          right: 0,
          bottom: 0,
          containLabel: true
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)',
          textStyle: {
            fontSize: 12
          }
        },
        series: [{
          name: '金额分布',
          type: 'pie',
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          label: {
            show: true,
            position: 'outside',
            color: '#475669',
            formatter: '{b}\n{d}%',
            lineHeight: 16,
            rich: {
              a: {
                color: '#475669'
              }
            }
          },
          labelLine: {
            length: 20,
            length2: 10
          },
          data: [
            { value: 1358, name: '1000至2000' },
            { value: 1548, name: '4000以上' },
            { value: 1548, name: '3000至4000' },
            { value: 1548, name: '1000以下' },
            { value: 1548, name: '2000至3000' }
          ],
          color: ['#55acee', '#4cdfc0', '#ff9945', '#facf2a', '#788cf0']
        }],
        legend: {
          orient: 'vertical',
          right: 0,
          bottom: 0,
          icon: 'rect',
          itemWidth: 15,
          itemHeight: 8,
          itemGap: 8,
          zlevel: 10,
          data: ['1000至2000', '4000以上', '3000至4000', '1000以下', '2000至3000'],
          textStyle: {
            fontSize: 12,
            color: '#8492a6'
          }
        }
      })
    }
  }
}
</script>